<template>
  <div class="app-container">
    <el-row>
      <el-card style="margin-bottom: 10px;">
        <el-col :span="24" :xs="24">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                   label-width="68px">
            <el-form-item label="标题" prop="recordTitle">
              <el-input
                v-model="queryParams.recordTitle"
                placeholder="请输入标题"
                clearable
              />
            </el-form-item>
            <el-form-item label="问题层级" prop="problemLevel">
              <el-select v-model="queryParams.problemLevel" placeholder="请选择问题层级" clearable>
                <el-option
                  v-for="dict in dict.type.apply_level"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="进度状态" prop="taskStatus">
              <!--<el-select v-model="queryParams.taskStatus" placeholder="请选择进度状态" clearable>-->
              <!--<el-option-->
              <!--v-for="dict in dict.type.meet_process_status"-->
              <!--:key="dict.value"-->
              <!--:label="dict.label"-->
              <!--:value="dict.value"-->
              <!--/>-->
              <!--</el-select>-->
              <el-select v-model="queryParams.checkStatus" placeholder="请选择进度状态" clearable>
                <el-option label="全部" value=""/>
                <el-option label="进行中" value="1"/>
                <el-option label="已完成" value="2"/>
                <el-option label="已撤回" value="3"/>
                <el-option label="未开始" value="4"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-card>

      <el-col :span="11" :xs="24">
        <el-card style="margin-right: 10px;height: 750px;">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['conference:applyTask:add']"
          >新 增
          </el-button>
          <el-button v-if="userLevel > 2"
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['conference:applyTask:export']"
          >导出
          </el-button>
          <el-table v-loading="loading" :data="applyTaskList" border>
            <!--<el-table-column label="序号" type="index" align="center">-->
              <!--<template slot-scope="scope">-->
                <!--<span>{{ scope.$index + 1 }}</span>-->
              <!--</template>-->
            <!--</el-table-column>-->
            <el-table-column label="标题" align="center" prop="recordTitle" width="120"  :show-overflow-tooltip="true">
              <template slot-scope="scope">
                <span>{{ scope.row.recordTitle }}</span>
              </template>
            </el-table-column>
            <el-table-column label="问题层级" align="center" prop="problemLevel" width="80">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.apply_level" :value="scope.row.problemLevel"/>
              </template>
            </el-table-column>

            <el-table-column label="创建人" align="center" prop="userName" width="60">
              <template slot-scope="scope">
                <span>{{ scope.row.userName }}</span>
              </template>
            </el-table-column>
            <el-table-column label="申请时间" align="center" prop="applicantTime" width="120">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.applicantTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="紧急度" align="center" prop="urgency" width="60">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.meet_urgency"
                          :value="scope.row.urgency"
                          :style="{ color: scope.row.urgency === '高' ? 'red' : '' }"
                          :class="{ 'font-bold': scope.row.urgency === '高' }"
                />
              </template>
            </el-table-column>
            <el-table-column label="进度状态" align="center" prop="taskStatus" width="80">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.meet_execution_status"
                          :value="scope.row.checkStatus"
                />
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-search"
                  @click="clickRow(scope.row)"
                  v-hasPermi="['conference:applyTask:edit']"
                >查看
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['conference:applyTask:edit']"
                >修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['conference:applyTask:remove']"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>

      </el-col>
      <el-col :span="13" :xs="24">
        <el-card style="padding-right: 0px;height: auto">
          <!-- 添加或修改我的申请对话框 -->
          <div :title="title" :visible.sync="open" width="500px" append-to-body>
            <h1 style="text-align: center"><b>发起信息</b></h1>
            <el-form ref="form" :model="form" :rules="rules" label-width="150px">
              <el-row :gutter="20" type="flex" style="flex-wrap:wrap">
                <el-col :span="24">
                  <el-form-item label="标题" prop="recordTitle">
                    <el-input v-model="form.recordTitle" placeholder="请输入申请标题"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="紧急度" prop="urgency">
                    <el-select v-model="form.urgency" placeholder="请选择紧急度"
                    >
                      <el-option
                        v-for="dict in dict.type.meet_urgency"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请类型 " prop="recordType">
                    <el-select v-model="form.recordType">
                      <el-option label="产品相关" value="1"/>
                      <el-option label="非产品相关" value="2"/>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请人" prop="userName">
                    <el-input v-model="form.userName" placeholder="请输入申请人" style="width: 220px"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请人部门" prop="applicantDetpCode">
                    <el-select v-model="form.applicantDetpCode" ref='selectApplicantDetpCode'
                               placeholder="请选择归属部门">
                      <el-option
                        v-for="item in deptOptions"
                        :key="item.id"
                        :label="item.label"
                        :value="item.id"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="问题层级" prop="problemLevel">
                    <el-select v-model="form.problemLevel" placeholder="请选择问题层级"
                               >
                      <el-option
                        v-for="dict in dict.type.apply_level"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="受理部门" prop="approverUserId">
                    <el-select v-model="form.approverUserId" ref='selectPerson' placeholder="请选择受理部门"
                               filterable>
                      <el-option
                        v-for="item in userList"
                        :key="item.userId"
                        :label="item.nickName"
                        :value="item.userId"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="申请时间" prop="applicantTime">
                    <el-date-picker clearable
                                    v-model="form.applicantTime"
                                    type="datetime"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    placeholder="请选择申请时间">
                    </el-date-picker>
                  </el-form-item>
                </el-col>

<!--                <el-col :span="24">
                  <el-form-item label="班组部门" prop="workgroupDeptCode">
                    <el-select v-model="form.workgroupDeptCode" ref='selectWorkgroupDeptCode'
                               placeholder="请选择归属部门">
                      <el-option
                        v-for="item in deptOptions"
                        :key="item.id"
                        :label="item.label"
                        :value="item.id"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>-->

                <el-col :span="12" v-if="form.recordType == '1'">
                  <el-form-item label="计划交货日期" prop="plannedDeliveryTime">
                    <el-date-picker clearable
                                    v-model="form.plannedDeliveryTime"
                                    type="datetime"
                                    value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择计划交货日期"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="12" v-if="form.recordType == '1'">
                  <el-form-item label="产品大类" prop="productCategory">
                    <el-select v-model="form.productCategory" placeholder="请选择产品大类">
                      <el-option
                        v-for="dict in dict.type.product_category"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="订单号" prop="orderNo" v-if="form.recordType == '1'">
                    <el-input v-model="form.orderNo" placeholder="请输入订单号"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="生产批号" prop="produceBathNo" v-if="form.recordType == '1'">
                    <el-input v-model="form.produceBathNo" placeholder="请输入生产批号"/>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="物料号" prop="materialNo" v-if="form.recordType == '1'">
                    <el-input v-model="form.materialNo" placeholder="请输入物料号"/>
                  </el-form-item>
                </el-col>

              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="问题描述" prop="problemRemark">
                    <el-input v-model="form.problemRemark" :rows="6" type="textarea" placeholder="请输入内容"/>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <el-form-item label="附件上传" prop="fileUrl">
                    <file-upload v-model="form.fileUrl"/>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align: center" v-show="submitDiv">
              <el-button type="primary" @click="stagingForm">暂 存</el-button>
              <el-button type="primary" @click="submitForm">提 交</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

  </div>
</template>

<script>
import {
  addApplyTask,
  delApplyTask,
  getApplyTask,
  listApplyTask,
  stagingApplyTask,
  updateApplyTask
} from "@/api/conference/applyTask";
import {deptTreeSelect, getUserByUserName} from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import moment from '@/utils/moment'

export default {
  name: "ApplyTask",
  components: {Treeselect},
  dicts: ['apply_level', 'product_category', 'meet_process_status', 'meet_execution_status','meet_urgency'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 我的申请表格数据
      applyTaskList: [],
      // 弹出层标题
      title: "",
      userLevel:null,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicantUserName: null,
        applicantDeptName: null,
        problemLevel: null,
        productCategory: null,
        taskStatus: null,
        problemType: null,
        checkStatus: null,
        meetSimFirstName: null,
      },
      // 表单参数
      form: {applicantTime: moment().format("YYYY-MM-DD HH:mm:ss")},
      // 表单校验
      rules: {
        recordTitle: [
          {required: true, message: '请输入标题', trigger: 'change'}
        ],
        userName: [
          {required: true, message: '请选择申请人', trigger: 'change'}
        ],
        recordType: [
          {required: true, message: '请选择申请类型', trigger: 'change'}
        ],
        urgency: [
          {required: true, message: '请选择紧急度', trigger: 'change'}
        ],
        applicantDetpCode: [
          {required: true, message: '请选择归属部门', trigger: 'change'}
        ],
        approverUserId: [
          {required: true, message: '请选择层级审批人', trigger: 'change'}
        ],
       /* workgroupDeptCode: [
          {required: true, message: '请选择班组部门', trigger: 'change'}
        ],*/
        applicantTime: [
          {required: true, message: '请填写申请时间', trigger: 'change'}
        ],
        plannedDeliveryTime: [
          {required: true, message: '请填写计划交货时间', trigger: 'change'}
        ],
        problemLevel: [
          {required: true, message: '请选择问题层级', trigger: 'change'}
        ],
        productCategory: [
          {required: true, message: '请选择产品大类', trigger: 'change'}
        ],
        orderNo: [
          {required: true, message: '请填写订单号', trigger: 'change'}
        ],
        produceBathNo: [
          {required: true, message: '请填写生产批号', trigger: 'change'}
        ],
        materialNo: [
          {required: true, message: '请填写物料号', trigger: 'change'}
        ],
        problemRemark: [
          {required: true, message: '请填写问题描述', trigger: 'change'}
        ],
      },
      // 员工信息集合
      userList: undefined,
      deptOptions: undefined,
      submitDiv: true,
      tempUserData: '',
    };
  },
  created() {
    this.getDeptTree();
    this.queryUser();
    this.getList();
    this.form.applicantUserId = this.$store.state.user.id
    this.form.applicantUserName = this.$store.state.user.nickName
    if(this.$store.state.user.id === 1){
      this.form.applicantDetpCode = ''
      // this.form.workgroupDeptCode = ''
    }else{
    this.form.applicantDetpCode = this.$store.state.user.deptId
    }
    this.userLevel = this.$store.state.user.userLevel
  },
  methods: {
    clickRow(row) {
      this.submitDiv = false;
      this.queryTaskInfo(row);
    },
    handleSelectLevel(level) {
      const dataReq = {
        userName: '',
        userLevel: level
      }
      getUserByUserName(dataReq).then(response => {
        this.userList = response.data;
      });
    },
    handleSelectUser(userId, levelType) {
      const user = this.userList.find(u => u.userId === userId);
      if (user) {
        if (levelType === '1') {
          this.form.applicantUserName = user.nickName;
        }
      }
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        console.log(response.data)
        this.deptOptions = response.data[0].children;
      });
    },
    queryUser() {
      const dataReq = {
        userName: ''
      }
      getUserByUserName(dataReq).then(response => {
        this.userList = response.data;
      });
    },
    /** 查询我的申请列表 */
    getList() {
      this.loading = true;
      listApplyTask(this.queryParams).then(response => {
        this.applyTaskList = response.rows;
        console.log(this.applyTaskList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applicantUserId: null,
        applicantUserName: null,
        applicantDetpCode: null,
        applicantDeptName: null,
        // workgroupDeptCode: null,
       // workgroupDeptName: null,
        problemLevel: null,
        approversNos: [],
        applicantTime: null,
        productCategory: null,
        orderNo: null,
        produceBathNo: null,
        materialNo: null,
        problemRemark: null,
        taskStatus: null,
        kpiName: null,
        handingDeptCode: null,
        handingDeptName: null,
        personLiableNo: null,
        personLiableName: null,
        problemType: null,
        planFinishTime: null,
        actualFinishTime: null,
        checkStatus: null,
        autoUpgradation: null,
        upgradationDay: null,
        plannedDeliveryTime: null,
        userName: null,
        fileUrl: null,
        urgency: '高'
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.submitDiv = true;
      this.$modal.msgSuccess("请填写发起信息");
      this.reset();
      this.open = true;
      this.form.applicantTime = moment().format("YYYY-MM-DD HH:mm:ss")
      this.form.applicantUserId = this.$store.state.user.id
      this.form.applicantUserName = this.$store.state.user.nickName
      //管理员登录新增时 部门默认为空
      if(this.$store.state.user.id === 1){
        this.form.applicantDetpCode = ''
        this.form.applicantDeptName = ''
      }else{
        this.form.applicantDetpCode = this.$store.state.user.deptId
        this.form.applicantDeptName = this.$store.state.user.deptName
      }
      this.title = "添加我的申请";
    },
    queryTaskInfo(row) {
      this.submitDiv = false;
      this.reset();
      const id = row.id || this.ids
      const taskStatus = row.taskStatus
      getApplyTask(id, taskStatus).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改我的申请";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // if (row.taskStatus >= 1) {
      //   return this.$modal.msgWarning("流程已发起不可再修改");
      // }
      this.submitDiv = true;
      this.reset();
      const id = row.id || this.ids
      const taskStatus = row.taskStatus
      getApplyTask(id, taskStatus).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改我的申请";
      });
    },
    //暂存
    stagingForm() {
      stagingApplyTask(this.form).then(response => {
        this.$modal.msgSuccess("暂存成功");
        this.open = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const approverUserName = this.$refs.selectPerson.selected.label
          const applicantDeptName = this.$refs.selectApplicantDetpCode.selected.label
          // const workgroupDeptName = this.$refs.selectWorkgroupDeptCode.selected.label
          this.form.approverUserName = approverUserName
          this.form.applicantDeptName = applicantDeptName
          // this.form.workgroupDeptName = workgroupDeptName
          if (this.form.id != null) {
            updateApplyTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplyTask(this.form).then(response => {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return delApplyTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('conference/applyTask/export', {
        ...this.queryParams
      }, `applyTask_${new Date().getTime()}.xlsx`)
    }
  }
}
;
</script>

<style>

</style>

